package controller;

import model.Video;
import service.VideoService;
import view.FrontView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.List;
import javax.swing.DefaultListModel;

public class FrontController {
    private final FrontView view;
    private final VideoService videoService;
    private static final Logger LOGGER = Logger.getLogger(FrontController.class.getName());

    public FrontController(FrontView view) {
        this.view = view;
        this.videoService = VideoService.getInstance();

        System.out.println("初始化FrontController"); // 调试输出
        updateVideoList();

        // 添加定时刷新（每5秒检查一次更新）
        new Timer(5000, e -> updateVideoList()).start();

        view.addPlayButtonListener(new PlayButtonListener());

        // 初始化视频列表
        updateVideoList();

        // 添加事件监听
        view.addPlayButtonListener(new PlayButtonListener());
        view.addStopButtonListener(new StopButtonListener());
        view.addNextButtonListener(new NextButtonListener());
        // 添加暂停按钮监听
        view.addPauseButtonListener(e -> view.togglePause());
    }

    /**
     * 更新视频列表
     */
    private void updateVideoList() {
        //System.out.println("调用updateVideoList()"); // 调试输出
        try {
            List<Video> videos = videoService.getAllVideos();
            //System.out.println("从服务获取到 " + videos.size() + " 个视频"); // 调试输出

            if (videos == null || videos.isEmpty()) {
                LOGGER.warning("获取到的视频列表为空");
                JOptionPane.showMessageDialog(view,
                        "没有可用的视频",
                        "提示",
                        JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            // 调用视图方法更新列表
            //System.out.println("准备调用view.setVideoList()"); // 调试输出
            view.setVideoList(videos);

        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "更新视频列表时发生错误", e);
            JOptionPane.showMessageDialog(view,
                    "加载视频列表失败: " + e.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    // 播放按钮监听器
    class PlayButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            Video selectedVideo = view.getSelectedVideo();
            if (selectedVideo != null) {
                System.out.println("播放视频: " + selectedVideo.getTitle()); // 调试输出
                view.playVideo(selectedVideo.getFilePath());
                videoService.getVideoById(selectedVideo.getId());
            } else {
                JOptionPane.showMessageDialog(view,
                        "请先选择一个视频",
                        "提示",
                        JOptionPane.WARNING_MESSAGE);
            }
        }
    }

    // 停止按钮监听器
    class StopButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (view.getCurrentPlayer() != null) {
                view.getCurrentPlayer().stop();
            }
        }
    }

    // 下一首按钮监听器
    class NextButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            view.playNextVideo();
        }
    }
}